﻿/*
 * Copyright © 2011 Tristan Reeves, treeves@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using NUnit.Framework;
using Spring.Context.Support;
using Sprinkles.Tests.DeploymentHosts;

namespace Sprinkles.Tests.UnitTests
{
    [TestFixture]
    public class MultipleInstanceEventTester
    {
        [Test]
        public void DoesMultipleCallsToObjectPostProcessor()
        {
            var nam = "PrototypeHost.obj";
            PrototypeHost.POST_CTR = 0;
            PrototypeHost.PRE_CTR = -99;
            var ctx = new XmlApplicationContext(@"config\DeployTest1.xml");
            Assert.That(PrototypeHost.POST_CTR, Is.EqualTo(0));
            Assert.That(PrototypeHost.PRE_CTR, Is.EqualTo(-99));

            var obj1 = ctx[nam] as PrototypeHost.PrototypeObject;
            Assert.That(PrototypeHost.POST_CTR, Is.EqualTo(1));
            Assert.That(PrototypeHost.PRE_CTR, Is.EqualTo(-100));

            var obj2 = ctx[nam] as PrototypeHost.PrototypeObject;
            Assert.That(PrototypeHost.POST_CTR, Is.EqualTo(2));
            Assert.That(PrototypeHost.PRE_CTR, Is.EqualTo(-101));
        }

        [Test]
        public void CanAccessTypesafeObjectInPostProcessor()
        {
            var nam = "PrototypeHost.obj";
            PrototypeHost.PREINIT_MESSAGES.Clear();
            PrototypeHost.POSTINIT_MESSAGES.Clear();
            var ctx = new XmlApplicationContext(@"config\DeployTest1.xml");
            Assert.That(PrototypeHost.PREINIT_MESSAGES, Is.Empty);
            Assert.That(PrototypeHost.POSTINIT_MESSAGES, Is.Empty);

            var obj1 = ctx[nam] as PrototypeHost.PrototypeObject;
            Assert.That(PrototypeHost.PREINIT_MESSAGES.Count, Is.EqualTo(1));
            Assert.That(PrototypeHost.PREINIT_MESSAGES[0], Is.EqualTo("pre: " + obj1.Text));
            Assert.That(PrototypeHost.PREINIT_MESSAGES[0], Is.EqualTo("pre: hola.1"));
            Assert.That(obj1.Pretext, Is.EqualTo("pre: hola.1"));

            Assert.That(PrototypeHost.POSTINIT_MESSAGES.Count, Is.EqualTo(1));
            Assert.That(PrototypeHost.POSTINIT_MESSAGES[0], Is.EqualTo("post: " + obj1.Text));
            Assert.That(PrototypeHost.POSTINIT_MESSAGES[0], Is.EqualTo("post: hola.1"));
            Assert.That(obj1.Posttext, Is.EqualTo("post: hola.1"));

            var obj2 = ctx[nam] as PrototypeHost.PrototypeObject;
            Assert.That(PrototypeHost.PREINIT_MESSAGES.Count, Is.EqualTo(2));
            Assert.That(PrototypeHost.PREINIT_MESSAGES[0], Is.EqualTo("pre: " + obj1.Text));
            Assert.That(PrototypeHost.PREINIT_MESSAGES[0], Is.EqualTo("pre: hola.1"));
            Assert.That(obj1.Pretext, Is.EqualTo("pre: hola.1"));
            Assert.That(PrototypeHost.PREINIT_MESSAGES[1], Is.EqualTo("pre: " + obj2.Text));
            Assert.That(PrototypeHost.PREINIT_MESSAGES[1], Is.EqualTo("pre: hola.2"));
            Assert.That(obj2.Pretext, Is.EqualTo("pre: hola.2"));

            Assert.That(PrototypeHost.POSTINIT_MESSAGES.Count, Is.EqualTo(2));
            Assert.That(PrototypeHost.POSTINIT_MESSAGES[0], Is.EqualTo("post: " + obj1.Text));
            Assert.That(PrototypeHost.POSTINIT_MESSAGES[0], Is.EqualTo("post: hola.1"));
            Assert.That(obj1.Posttext, Is.EqualTo("post: hola.1"));
            Assert.That(PrototypeHost.POSTINIT_MESSAGES[1], Is.EqualTo("post: " + obj2.Text));
            Assert.That(PrototypeHost.POSTINIT_MESSAGES[1], Is.EqualTo("post: hola.2"));
            Assert.That(obj2.Posttext, Is.EqualTo("post: hola.2"));
        }

        [Test]
        public void ContextIsAccessibleInPostProcessor()
        {
            PrototypeHost.DO_INCLUDE = true;
            try
            {
                var nam = "PrototypeHost.object2";
                var ctx = new XmlApplicationContext(@"config\DeployTest1.xml");

                var obj = ctx[nam] as PrototypeHost.Object2;
                Assert.That(obj.PreLink.Name, Is.EqualTo("hello.1"));
                Assert.That(obj.PostLink.Name, Is.EqualTo("hello.2"));

                var obj2 = ctx[nam] as PrototypeHost.Object2;
                Assert.That(obj2.PreLink.Name, Is.EqualTo("hello.1"));
                Assert.That(obj2.PostLink.Name, Is.EqualTo("hello.2"));
            }
            finally
            {
                PrototypeHost.DO_INCLUDE = false;
            }
        }
    }
}
